﻿--[[
   @brief    deal with lua event.
   @author   sogoodchen
   @version  1.0
   @date     2015-03-09
--]]

event_center =
{
    add_listener = function(event_id, callback)
        if event_center.events[event_id] == nil then
            event_center.events[event_id] = {};
        end;
        if "desc_mgr.lua_event_callback" == callback then
            -- 避免加多个lua_event_callback
            local found = false;
            local callbacks = event_center.events[event_id];
            for idx_, callback_ in ipairs(callbacks) do
                if "desc_mgr.lua_event_callback" == callback_ then
                    found = true;
                    break;
                end
            end
            if false == found then
                table.insert(event_center.events[event_id], callback);
            end
        else
            table.insert(event_center.events[event_id], callback);
        end
    end;

    remove_listener = function(event_id, callback)
        base_utility.log('[event_center] remove_listener: event_id='..event_id..', call_back='..callback);
        if event_center.events[event_id] == nil then
            return;
        end;
        for idx_, callback_ in ipairs(event_center.events[event_id]) do
            if callback_ == callback then
                table.remove(event_center.events[event_id], idx_);
                break;
            end
        end
    end;

    send_event = function(event_id, param)
        base_utility.log('[event_center] send_event: event_id='..event_id);
        local events_tmp = base_utility.copy_table(event_center.events);
        if events_tmp[event_id] == nil then
            return;
        end;
        
        local callbacks = events_tmp[event_id];
        for idx_, callback_ in ipairs(callbacks) do
            local param_type = type(param);
            if type(param) == 'string' and string.find(param, "clientkey") == nil then
                base_utility.log('[event_center] send_event['..idx_..']: event_id='..event_id..', callback='..callback_);
            else
                base_utility.log('[event_center] send_event['..idx_..']: event_id='..event_id..', callback='..callback_..', param_type='..param_type);
            end;
            
            event_center.event_param = param;
            local script_tmp = callback_ .. '(\''..event_id..'\', event_center.event_param)';
            local func = loadstring(script_tmp);
            func();
        end;
    end;

        -- 发送异步事件
    post_event = function(uEventID, sParam, sameEvtPostOnce)
        local   cEvent = {};

        cEvent["Event"] = uEventID;
        cEvent["Param"] = sParam;


        if true == sameEvtPostOnce then
            local is_event_exit = false;
            for k, v in pairs(event_center.zAsyncEvts) do
                if v.Event == uEventID and v.Param == sParam then
                    is_event_exit = true;
                    break;
                end
            end

            -- 事件ID不存在才插进表中
            if false == is_event_exit then
               table.insert(event_center.zAsyncEvts, cEvent);
            end
        else
            table.insert(event_center.zAsyncEvts, cEvent);
        end

        -- 开启定时器
        event_center.StartTimer();
    end;

    -- 异步定时器
    StartTimer = function()
        local tpfui = ierd_tgp.tpf_ui.GetTPFUI();

        if nil ~= tpfui then
            tpfui:AddInterval("event_center.OnTimer", 100, 0);
        end;
    end;

    -- 停止定时器
    StopTimer = function()
        local tpfui = ierd_tgp.tpf_ui.GetTPFUI();

        if nil ~= tpfui then
            tpfui:RemoveInterval("event_center.OnTimer", 0);
        end;
    end;

    -- 异步事件处理
    OnTimer = function(UserData, TimeStamp)
        event_center.StopTimer();

        local event_list = {};
        for k, v in pairs(event_center.zAsyncEvts) do
            table.insert(event_list, v);
        end

        event_center.zAsyncEvts = {};

        for k, v in pairs(event_list) do
            event_center.send_event(v["Event"], v["Param"]);
        end

    end;

    -- 异步事件
    zAsyncEvts = {};

    events = {}; -- id={callback1, callback2,...}
    event_param = '{}';
};